﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdEditDrawProfileBase.h"
#include "CmdStatusUtils.h"
#include "IInstance.h"
#include "ContainerUtil.h"
#include "ISelection.h"
#include "IDocument.h"
#include "IElementBasicInformation.h"
#include "IGraphicsNodeReference.h"
#include "UniIdentity.h"
#include "UiDocumentViewUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

CmdEditDrawProfileBase::CmdEditDrawProfileBase(const std::wstring& cmdId)
    : GbmpCommandBase(cmdId)
{
}

bool CmdEditDrawProfileBase::IsEnabled() const
{
    if (!CommandBase::IsEnabled())
    {
        return false;
    }

    CMD_ONLY_ENABLE_IN_PROJECT_ENVIRONMENT
}

bool CmdEditDrawProfileBase::IsVisible() const
{
    IInstance* editingInstance = GetSelectedInstance();
    if (editingInstance && editingInstance->GetElementId().IsValid())
    {
        return true;
    }

    return false;
}

IInstance* CmdEditDrawProfileBase::GetSelectedInstance() const
{
    const std::set<OwnerPtr<IGraphicsNodeReference>, OwnerPtrContainerUtil::LessPredicate<IGraphicsNodeReference> >& selectedObjs
        = ISelection::Get()->GetGraphicsNodeReferences();
    if (selectedObjs.size() == 1)
    {
        const IDocument* pDocument = UiDocumentViewUtils::GetCurrentDocument();
        if (!pDocument)
        {
            return nullptr;
        }

        ElementId elementId = (*selectedObjs.begin())->GetElementId();
        IInstance* pInstance = quick_cast<IInstance>(pDocument->GetElement(elementId));
        if (!pInstance)
        {
            return nullptr;
        }
        if (pInstance->GetBasicInformation()->GetCategoryUid() == this->GetCategory())
        {
            return pInstance;
        }
    }

    return nullptr;
}
